﻿using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using ClassLibrary1;
using DependencyAnalysis;
using DependencyAnalysis.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace IntegrationTests
{
    [TestClass]
    public class SerializationTest
    {
        [TestMethod]
        public void CreateCodeSmellTest()
        {
            var setup = Setup.BuildStandardSetup();
            setup.StartAnalysis(new[] { typeof(Class1).Assembly });
            setup.CreateReportXml<CodeSmellReport>("testsmell.xml", string.Empty);

            using (FileStream fileSteam = File.OpenRead("testsmell.xml"))
            {
                var settings = new XmlReaderSettings();
                settings.ConformanceLevel = ConformanceLevel.Document;

                using (XmlReader reader = XmlReader.Create(fileSteam, settings))
                {
                    reader.MoveToContent();
                    var node = XNode.ReadFrom(reader) as XElement;
                    Assert.AreEqual("Report", node.Name);
                    var attributes = node.Attributes();
                    Assert.IsTrue(attributes.Any(x => x.Name == "Name" && x.Value == "Code Smell"));
                    var children = node.Nodes().Cast<XElement>().ToArray();
                    Assert.IsTrue(children.Any(x => x.Name == "Members"));
                    Assert.IsTrue(children.Any(x => x.Name == "Types"));
                    Assert.IsTrue(children.Any(x => x.Name == "Submodules"));
                    Assert.IsTrue(children.Any(x => x.Name == "Modules"));
                }
            }
        }

        [TestMethod]
        public void CreateDuplicationTest()
        {
            var setup = Setup.BuildStandardSetup();
            setup.StartAnalysis(new[] { typeof(Class1).Assembly });
            setup.CreateReportXml<DuplicationReport>("testduplicate.xml", string.Empty);

            using (FileStream fileSteam = File.OpenRead("testduplicate.xml"))
            {
                var settings = new XmlReaderSettings();
                settings.ConformanceLevel = ConformanceLevel.Document;

                using (XmlReader reader = XmlReader.Create(fileSteam, settings))
                {
                    reader.MoveToContent();
                    var node = XNode.ReadFrom(reader) as XElement;
                    Assert.AreEqual("Report", node.Name);
                    var attributes = node.Attributes();
                    Assert.IsTrue(attributes.Any(x => x.Name == "Name" && x.Value == "Code Duplication"));
                    var children = node.Nodes().Cast<XElement>().ToArray();
                    Assert.IsTrue(children.Any(x => x.Name == "Duplicates"));
                }
            }
        }

        [TestMethod]
        public void CreateMetricsReportTest()
        {
            var setup = Setup.BuildStandardSetup();
            setup.StartAnalysis(new[] { typeof(Class1).Assembly });
            setup.CreateReportXml<MetricsReport>("testmetrics.xml", string.Empty);

            using (FileStream fileSteam = File.OpenRead("testmetrics.xml"))
            {
                var settings = new XmlReaderSettings();
                settings.ConformanceLevel = ConformanceLevel.Document;

                using (XmlReader reader = XmlReader.Create(fileSteam, settings))
                {
                    reader.MoveToContent();
                    var node = XNode.ReadFrom(reader) as XElement;
                    Assert.AreEqual("Report", node.Name);
                    var attributes = node.Attributes();
                    Assert.IsTrue(attributes.Any(x => x.Name == "Name" && x.Value == "Metrics Summary"));
                    var children = node.Nodes().Cast<XElement>().ToArray();
                    Assert.IsTrue(children.Any(x => x.Name == "Modules"));
                }
            }
        }
    }
}
